<?php

class Email
{
    //注册首页
    function index()
    {
        include './view/index/index.php';
    }

    //发送验证码
    public function sendCode()
    {
        $code = substr(md5(rand(1000, 9999)), 1, 6);
        $email = $_POST['email'];
        //加载发送邮件类
        require "./mailClass.php";
        $res = mailClass::sendEmail($email, $code);
        if ($res) {
            $resData['status'] = true;
            $resData['msg'] = '发送成功';
            //记录session
            $_SESSION['email'] = $email;
            $_SESSION['code'] = $code;
            $_SESSION['codeCreateTime'] = time();
        } else {
            $resData['status'] = false;
            $resData['msg'] = '发送失败';
        }
        echo json_encode($resData);
    }

    //提交注册
    public function register()
    {
        $postData = $_POST;
        $sessionData = $_SESSION;

        if ($postData['email'] == $sessionData['email'] && $postData['emailCode'] == $sessionData['code'] && $sessionData['codeCreateTime'] + 60 * 10 > time()) {
            echo '恭喜您 注册成功，准备发车咯';
        } else {
            echo '抱歉，您的操作有误，请重新注册';
        }
    }

}
